package com.techysky.util;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.session.mgt.SimpleSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.techysky.entity.User;
import com.techysky.entity.UserLockManage;

import com.techysky.repository.UserLockManageRepository;
import com.techysky.shiro.ApplicationContextRegister;

import com.techysky.util.PowCalculate.Powdll;

public class MyHandlerInterceptor implements HandlerInterceptor{
	private static Log log = LogFactory.getLog(MyHandlerInterceptor.class);
    /**
     * 存储不拦截路径
     */
    private static final String[] IGNORE_URI = {"/login","/activate","/admin"};
    //在请求处理之前进行调用（Controller方法调用之前）
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
  
        boolean flag=false;
        String servletPath=req.getServletPath();
    
        //遍历数组确定该路径是否需要拦截
        for(String str:IGNORE_URI){
            if(servletPath.contains(str)){
        /*    	UserLockManageRepository us = ApplicationContextRegister.getBean(UserLockManageRepository.class);
    			List<UserLockManage> ums = us.findAll();
    			if (ums != null && ums.size() >= 1) {
    			
    				flag = false;
    			} else {
    			*/
    				flag = true;
    			//}
        
            }
        }
        //拦截请求
		if (!flag) {
	
	/*		if(servletPath.contains("/powTechysky")){
				int size = Powdll.Instance.BatteryLifePercent();
				resp.setCharacterEncoding("utf-8");
				resp.setContentType("application/json; charset=utf-8");
				PrintWriter writer = resp.getWriter();
				writer.write(String.valueOf(size));
				flag = false;
			}else{}*/
			flag = true;
		
		
		}
        return flag;// 只有返回true才会继续向下执行，返回false取消当前请求
    }
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
}
